package com.tool.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import com.tool.DataCenterPersistence;
import com.tool.DataCenterVO;
import com.tool.LicenseMgmt;
import com.tool.ResourceBundleHelper;

/**
 * 数据中列表界面
 * 
 * @author Evan.Hu
 * 
 */
public class DCListUI extends CoreUI {

	private static final long serialVersionUID = -9032254160569212038L;
	private List<DataCenterVO> dataList = new ArrayList<DataCenterVO>();

	JButton closeBtn;
	JButton registerBtn;
	JButton unRegisterBtn;

	JTable tableView;

	public DCListUI() {
		super();
		setPreferredSize(new Dimension(660, 400));
		initUI();
	}

	private void initUI() {
		resHelper = new ResourceBundleHelper(DCListUI.class.getName());

		setTitle(resHelper.getString("Frame.title"));

		setLayout(null);

		// R1C1:
		registerBtn = new JButton();
		registerBtn.setBounds(new Rectangle(20, 20, 80, 25));
		registerBtn.setAction(new AbstractAction(resHelper.getString("registerBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					register_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		add(registerBtn, null);

		// R1C2:
		unRegisterBtn = new JButton();
		unRegisterBtn.setBounds(new Rectangle(110, 20, 80, 25));
		unRegisterBtn.setAction(new AbstractAction(resHelper.getString("unRegisterBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					unRegister_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		add(unRegisterBtn, null);
		// R1C3:
		closeBtn = new JButton();
		closeBtn.setAction(new AbstractAction(resHelper.getString("closeBtn")) {
			public void actionPerformed(ActionEvent e) {
				try {
					close_ActionPerformed(e);
				} catch (Exception e1) {
					handUIException(e1);
				}
			}
		});
		closeBtn.setBounds(new Rectangle(200, 20, 80, 25));
		add(closeBtn, null);

		final String[] names = { "编码", "名称", "类型", "数据库地址", "数据库名", "用户" };
		TableModel dataModel = new AbstractTableModel() {
			public int getColumnCount() {
				return names.length;
			}

			public int getRowCount() {
				return dataList.size();
			}

			public Object getValueAt(int row, int col) {
				Object value = null;
				if (col == 0) {
					value = dataList.get(row).getDcNumber();
				} else if (col == 1) {
					value = dataList.get(row).getDcName();
				} else if (col == 2) {
					value = dataList.get(row).getDbTypeName();
				} else if (col == 3) {
					final DataCenterVO vo = dataList.get(row);
					value = vo.getDbAddr() + ":" + vo.getDbPort();
				} else if (col == 4) {
					value = dataList.get(row).getDbName();
				} else if (col == 5) {
					value = dataList.get(row).getUserName();
				}
				return value;
			}

			public String getColumnName(int column) {
				return names[column];
			}

			public Class getColumnClass(int c) {
				return "a".getClass();
				// return getValueAt(0, c).getClass();
			}

			public boolean isCellEditable(int row, int col) {
				return false;
			}

			public void setValueAt(Object aValue, int row, int column) {
				// data[row][column] = aValue;
			}
		};

		tableView = new JTable(dataModel);
		tableView.setSelectionMode(0);
		tableView.setShowVerticalLines(true);
		JScrollPane scrollPane = new JScrollPane(tableView);

		JPanel tabPanel = new JPanel(new BorderLayout());
		tabPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
		tabPanel.add(scrollPane, BorderLayout.CENTER);

		tabPanel.setBounds(new Rectangle(18, 50, 580, 270));
		add(tabPanel, null);

	}

	public void onLoad() throws Exception {
		setResizable(false);
		dataList = DataCenterPersistence.getDCList();

		registerBtn.setEnabled(LicenseMgmt.isLicenseValid());
		unRegisterBtn.setEnabled(LicenseMgmt.isLicenseValid());
	}

	/**
	 * 关闭当前界面
	 * 
	 * @param e
	 */
	public void close_ActionPerformed(ActionEvent e) throws Exception {
		getWindow().dispose();
	}

	/**
	 * 注册
	 * 
	 * @param e
	 */
	public void register_ActionPerformed(ActionEvent e) throws Exception {
		if (getWindow() instanceof JFrame) {
			new DialogUIHelper((JFrame) getWindow(), new DCRegisterUI(), true).show();
		} else if (getWindow() instanceof JDialog) {
			new DialogUIHelper((JDialog) getWindow(), new DCRegisterUI(), true).show();
		}
		tableView.revalidate();

	}

	/**
	 * 反注册
	 * 
	 * @param e
	 */
	public void unRegister_ActionPerformed(ActionEvent e) throws Exception {
		final int selectedRow = tableView.getSelectedRow();
		if (selectedRow == -1 || selectedRow >= DataCenterPersistence.getDCList().size()) {
			throw new RuntimeException("请选中行记录");
		}
		DataCenterVO selectedVO = DataCenterPersistence.getDCList().get(selectedRow);
		int option = JOptionPane.showConfirmDialog(getWindow(), "确认反注册编码为[" + selectedVO.getDcNumber() + "]的数据中心");
		if (option == JOptionPane.OK_OPTION) {
			DataCenterPersistence.unRegister(selectedVO);
			JOptionPane.showMessageDialog(getWindow(), "反注册成功");
		}
	}

	public static void main(String[] args) {

		new DCListUI().showUI();
	}
}
